1   /*
2    * Copyright (C) 2006 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.base;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.annotations.GwtIncompatible;
21  import com.google.common.testing.NullPointerTester;
22  
23  import junit.framework.AssertionFailedError;
24  import junit.framework.TestCase;
25  
26  /**
27   * Unit test for {@link Preconditions}.
28   *
29   * @author Kevin Bourrillion
30   * @author Jared Levy
31   */
32  @GwtCompatible(emulated = true)
33  public class PreconditionsTest extends TestCase {
34    public void testCheckArgument_simple_success() {
35      Preconditions.checkArgument(true);
36    }
37  
38    public void testCheckArgument_simple_failure() {
39      try {
40        Preconditions.checkArgument(false);
41        fail("no exception thrown");
42      } catch (IllegalArgumentException expected) {
43      }
44    }
45  
46    public void testCheckArgument_simpleMessage_success() {
47      Preconditions.checkArgument(true, IGNORE_ME);
48    }
49  
50    public void testCheckArgument_simpleMessage_failure() {
51      try {
52        Preconditions.checkArgument(false, new Message());
53        fail("no exception thrown");
54      } catch (IllegalArgumentException expected) {
55        verifySimpleMessage(expected);
56      }
57    }
58  
59    public void testCheckArgument_nullMessage_failure() {
60      try {
61        Preconditions.checkArgument(false, null);
62        fail("no exception thrown");
63      } catch (IllegalArgumentException expected) {
64        assertEquals("null", expected.getMessage());
65      }
66    }
67  
68    public void testCheckArgument_complexMessage_success() {
69      Preconditions.checkArgument(true, "%s", IGNORE_ME);
70    }
71  
72    public void testCheckArgument_complexMessage_failure() {
73      try {
74        Preconditions.checkArgument(false, FORMAT, 5);
75        fail("no exception thrown");
76      } catch (IllegalArgumentException expected) {
77        verifyComplexMessage(expected);
78      }
79    }
80  
81    public void testCheckState_simple_success() {
82      Preconditions.checkState(true);
83    }
84  
85    public void testCheckState_simple_failure() {
86      try {
87        Preconditions.checkState(false);
88        fail("no exception thrown");
89      } catch (IllegalStateException expected) {
90      }
91    }
92  
93    public void testCheckState_simpleMessage_success() {
94      Preconditions.checkState(true, IGNORE_ME);
95    }
96  
97    public void testCheckState_simpleMessage_failure() {
98      try {
99        Preconditions.checkState(false, new Message());
100       fail("no exception thrown");
101     } catch (IllegalStateException expected) {
102       verifySimpleMessage(expected);
103     }
104   }
105 
106   public void testCheckState_nullMessage_failure() {
107     try {
108       Preconditions.checkState(false, null);
109       fail("no exception thrown");
110     } catch (IllegalStateException expected) {
111       assertEquals("null", expected.getMessage());
112     }
113   }
114 
115   public void testCheckState_complexMessage_success() {
116     Preconditions.checkState(true, "%s", IGNORE_ME);
117   }
118 
119   public void testCheckState_complexMessage_failure() {
120     try {
121       Preconditions.checkState(false, FORMAT, 5);
122       fail("no exception thrown");
123     } catch (IllegalStateException expected) {
124       verifyComplexMessage(expected);
125     }
126   }
127 
128   private static final String NON_NULL_STRING = "foo";
129 
130   public void testCheckNotNull_simple_success() {
131     String result = Preconditions.checkNotNull(NON_NULL_STRING);
132     assertSame(NON_NULL_STRING, result);
133   }
134 
135   public void testCheckNotNull_simple_failure() {
136     try {
137       Preconditions.checkNotNull(null);
138       fail("no exception thrown");
139     } catch (NullPointerException expected) {
140     }
141   }
142 
143   public void testCheckNotNull_simpleMessage_success() {
144     String result = Preconditions.checkNotNull(NON_NULL_STRING, IGNORE_ME);
145     assertSame(NON_NULL_STRING, result);
146   }
147 
148   public void testCheckNotNull_simpleMessage_failure() {
149     try {
150       Preconditions.checkNotNull(null, new Message());
151       fail("no exception thrown");
152     } catch (NullPointerException expected) {
153       verifySimpleMessage(expected);
154     }
155   }
156 
157   public void testCheckNotNull_complexMessage_success() {
158     String result = Preconditions.checkNotNull(
159         NON_NULL_STRING, "%s", IGNORE_ME);
160     assertSame(NON_NULL_STRING, result);
161   }
162 
163   public void testCheckNotNull_complexMessage_failure() {
164     try {
165       Preconditions.checkNotNull(null, FORMAT, 5);
166       fail("no exception thrown");
167     } catch (NullPointerException expected) {
168       verifyComplexMessage(expected);
169     }
170   }
171 
172   public void testCheckElementIndex_ok() {
173     assertEquals(0, Preconditions.checkElementIndex(0, 1));
174     assertEquals(0, Preconditions.checkElementIndex(0, 2));
175     assertEquals(1, Preconditions.checkElementIndex(1, 2));
176   }
177 
178   public void testCheckElementIndex_badSize() {
179     try {
180       Preconditions.checkElementIndex(1, -1);
181       fail();
182     } catch (IllegalArgumentException expected) {
183       // don't care what the message text is, as this is an invalid usage of
184       // the Preconditions class, unlike all the other exceptions it throws
185     }
186   }
187 
188   public void testCheckElementIndex_negative() {
189     try {
190       Preconditions.checkElementIndex(-1, 1);
191       fail();
192     } catch (IndexOutOfBoundsException expected) {
193       assertEquals("index (-1) must not be negative", expected.getMessage());
194     }
195   }
196 
197   public void testCheckElementIndex_tooHigh() {
198     try {
199       Preconditions.checkElementIndex(1, 1);
200       fail();
201     } catch (IndexOutOfBoundsException expected) {
202       assertEquals("index (1) must be less than size (1)",
203           expected.getMessage());
204     }
205   }
206 
207   public void testCheckElementIndex_withDesc_negative() {
208     try {
209       Preconditions.checkElementIndex(-1, 1, "foo");
210       fail();
211     } catch (IndexOutOfBoundsException expected) {
212       assertEquals("foo (-1) must not be negative", expected.getMessage());
213     }
214   }
215 
216   public void testCheckElementIndex_withDesc_tooHigh() {
217     try {
218       Preconditions.checkElementIndex(1, 1, "foo");
219       fail();
220     } catch (IndexOutOfBoundsException expected) {
221       assertEquals("foo (1) must be less than size (1)",
222           expected.getMessage());
223     }
224   }
225 
226   public void testCheckPositionIndex_ok() {
227     assertEquals(0, Preconditions.checkPositionIndex(0, 0));
228     assertEquals(0, Preconditions.checkPositionIndex(0, 1));
229     assertEquals(1, Preconditions.checkPositionIndex(1, 1));
230   }
231 
232   public void testCheckPositionIndex_badSize() {
233     try {
234       Preconditions.checkPositionIndex(1, -1);
235       fail();
236     } catch (IllegalArgumentException expected) {
237       // don't care what the message text is, as this is an invalid usage of
238       // the Preconditions class, unlike all the other exceptions it throws
239     }
240   }
241 
242   public void testCheckPositionIndex_negative() {
243     try {
244       Preconditions.checkPositionIndex(-1, 1);
245       fail();
246     } catch (IndexOutOfBoundsException expected) {
247       assertEquals("index (-1) must not be negative", expected.getMessage());
248     }
249   }
250 
251   public void testCheckPositionIndex_tooHigh() {
252     try {
253       Preconditions.checkPositionIndex(2, 1);
254       fail();
255     } catch (IndexOutOfBoundsException expected) {
256       assertEquals("index (2) must not be greater than size (1)",
257           expected.getMessage());
258     }
259   }
260 
261   public void testCheckPositionIndex_withDesc_negative() {
262     try {
263       Preconditions.checkPositionIndex(-1, 1, "foo");
264       fail();
265     } catch (IndexOutOfBoundsException expected) {
266       assertEquals("foo (-1) must not be negative", expected.getMessage());
267     }
268   }
269 
270   public void testCheckPositionIndex_withDesc_tooHigh() {
271     try {
272       Preconditions.checkPositionIndex(2, 1, "foo");
273       fail();
274     } catch (IndexOutOfBoundsException expected) {
275       assertEquals("foo (2) must not be greater than size (1)",
276           expected.getMessage());
277     }
278   }
279 
280   public void testCheckPositionIndexes_ok() {
281     Preconditions.checkPositionIndexes(0, 0, 0);
282     Preconditions.checkPositionIndexes(0, 0, 1);
283     Preconditions.checkPositionIndexes(0, 1, 1);
284     Preconditions.checkPositionIndexes(1, 1, 1);
285   }
286 
287   public void testCheckPositionIndexes_badSize() {
288     try {
289       Preconditions.checkPositionIndexes(1, 1, -1);
290       fail();
291     } catch (IllegalArgumentException expected) {
292     }
293   }
294 
295   public void testCheckPositionIndex_startNegative() {
296     try {
297       Preconditions.checkPositionIndexes(-1, 1, 1);
298       fail();
299     } catch (IndexOutOfBoundsException expected) {
300       assertEquals("start index (-1) must not be negative",
301           expected.getMessage());
302     }
303   }
304 
305   public void testCheckPositionIndexes_endTooHigh() {
306     try {
307       Preconditions.checkPositionIndexes(0, 2, 1);
308       fail();
309     } catch (IndexOutOfBoundsException expected) {
310       assertEquals("end index (2) must not be greater than size (1)",
311           expected.getMessage());
312     }
313   }
314 
315   public void testCheckPositionIndexes_reversed() {
316     try {
317       Preconditions.checkPositionIndexes(1, 0, 1);
318       fail();
319     } catch (IndexOutOfBoundsException expected) {
320       assertEquals("end index (0) must not be less than start index (1)",
321           expected.getMessage());
322     }
323   }
324 
325   public void testFormat() {
326     assertEquals("%s", Preconditions.format("%s"));
327     assertEquals("5", Preconditions.format("%s", 5));
328     assertEquals("foo [5]", Preconditions.format("foo", 5));
329     assertEquals("foo [5, 6, 7]", Preconditions.format("foo", 5, 6, 7));
330     assertEquals("%s 1 2", Preconditions.format("%s %s %s", "%s", 1, 2));
331     assertEquals(" [5, 6]", Preconditions.format("", 5, 6));
332     assertEquals("123", Preconditions.format("%s%s%s", 1, 2, 3));
333     assertEquals("1%s%s", Preconditions.format("%s%s%s", 1));
334     assertEquals("5 + 6 = 11", Preconditions.format("%s + 6 = 11", 5));
335     assertEquals("5 + 6 = 11", Preconditions.format("5 + %s = 11", 6));
336     assertEquals("5 + 6 = 11", Preconditions.format("5 + 6 = %s", 11));
337     assertEquals("5 + 6 = 11", Preconditions.format("%s + %s = %s", 5, 6, 11));
338     assertEquals("null [null, null]",
339         Preconditions.format("%s", null, null, null));
340     assertEquals("null [5, 6]", Preconditions.format(null, 5, 6));
341   }
342 
343   @GwtIncompatible("NullPointerTester")
344   public void testNullPointers() {
345     NullPointerTester tester = new NullPointerTester();
346     tester.testAllPublicStaticMethods(Preconditions.class);
347   }
348 
349   private static final Object IGNORE_ME = new Object() {
350     @Override public String toString() {
351       throw new AssertionFailedError();
352     }
353   };
354 
355   private static class Message {
356     boolean invoked;
357     @Override public String toString() {
358       assertFalse(invoked);
359       invoked = true;
360       return "A message";
361     }
362   }
363 
364   private static final String FORMAT = "I ate %s pies.";
365 
366   private static void verifySimpleMessage(Exception e) {
367     assertEquals("A message", e.getMessage());
368   }
369 
370   private static void verifyComplexMessage(Exception e) {
371     assertEquals("I ate 5 pies.", e.getMessage());
372   }
373 }